package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

/**
 * 设置/修改密码Activity
 */
public class SetPasswordActivity extends AppCompatActivity {

    private ParentalControlRepository repository;
    
    private TextView textTitle;
    private TextView textMessage;
    private StringBuilder password;
    private View[] dots;
    
    private boolean hasPassword; // 是否已有密码
    private boolean verifyingOldPassword; // 是否正在验证旧密码
    private String oldPassword; // 旧密码
    private String firstNewPassword; // 第一次输入的新密码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_verify);

        repository = ParentalControlRepository.getInstance(this);
        
        // 检查是否已有密码
        oldPassword = repository.getPassword();
        hasPassword = oldPassword != null && !oldPassword.isEmpty();
        
        initViews();
        setupListeners();
        updateUI();
    }

    private void initViews() {
        textTitle = findViewById(R.id.text_verify_title);
        textMessage = findViewById(R.id.text_verify_message);
        password = new StringBuilder();
        
        dots = new View[]{
            findViewById(R.id.dot1),
            findViewById(R.id.dot2),
            findViewById(R.id.dot3),
            findViewById(R.id.dot4)
        };
    }

    private void updateUI() {
        if (hasPassword) {
            if (verifyingOldPassword) {
                // 验证旧密码阶段
                textTitle.setText("输入密码");
                textMessage.setText("请输入原密码");
            } else {
                // 设置新密码阶段
                if (firstNewPassword == null) {
                    textTitle.setText("输入密码");
                    textMessage.setText("请输入新密码");
                } else {
                    textTitle.setText("输入密码");
                    textMessage.setText("请再次输入新密码");
                }
            }
        } else {
            // 首次设置密码
            if (firstNewPassword == null) {
                textTitle.setText("输入密码");
                textMessage.setText("输入密码启用智能控制");
            } else {
                textTitle.setText("输入密码");
                textMessage.setText("请再次输入新密码");
            }
        }
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        // 初始化状态
        if (hasPassword) {
            verifyingOldPassword = true;
        }
    }

    private void setupListeners() {
        View.OnClickListener numberListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (password.length() < 4) {
                    Button btn = (Button) v;
                    password.append(btn.getText());
                    updatePasswordDots(password.length());
                    
                    if (password.length() == 4) {
                        handlePasswordInput();
                    }
                }
            }
        };

        findViewById(R.id.btn_0).setOnClickListener(numberListener);
        findViewById(R.id.btn_1).setOnClickListener(numberListener);
        findViewById(R.id.btn_2).setOnClickListener(numberListener);
        findViewById(R.id.btn_3).setOnClickListener(numberListener);
        findViewById(R.id.btn_4).setOnClickListener(numberListener);
        findViewById(R.id.btn_5).setOnClickListener(numberListener);
        findViewById(R.id.btn_6).setOnClickListener(numberListener);
        findViewById(R.id.btn_7).setOnClickListener(numberListener);
        findViewById(R.id.btn_8).setOnClickListener(numberListener);
        findViewById(R.id.btn_9).setOnClickListener(numberListener);

        findViewById(R.id.btn_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (password.length() > 0) {
                    password.deleteCharAt(password.length() - 1);
                    updatePasswordDots(password.length());
                }
            }
        });
    }

    private void updatePasswordDots(int count) {
        for (int i = 0; i < dots.length; i++) {
            if (i < count) {
                dots[i].setBackgroundResource(R.drawable.password_dot_filled);
            } else {
                dots[i].setBackgroundResource(R.drawable.password_dot_empty);
            }
        }
    }

    private void handlePasswordInput() {
        String inputPassword = password.toString();
        
        if (hasPassword && verifyingOldPassword) {
            // 验证旧密码
            if (inputPassword.equals(oldPassword)) {
                // 旧密码正确，进入设置新密码阶段
                verifyingOldPassword = false;
                password.setLength(0);
                updatePasswordDots(0);
                updateUI();
            } else {
                // 旧密码错误
                Toast.makeText(this, "原密码错误，请重新输入", Toast.LENGTH_SHORT).show();
                password.setLength(0);
                updatePasswordDots(0);
            }
        } else {
            // 设置新密码
            if (firstNewPassword == null) {
                // 第一次输入新密码
                firstNewPassword = inputPassword;
                password.setLength(0);
                updatePasswordDots(0);
                updateUI();
            } else {
                // 第二次输入新密码，进行确认
                if (inputPassword.equals(firstNewPassword)) {
                    // 两次密码一致，保存
                    repository.setPassword(firstNewPassword);
                    Toast.makeText(this, "密码设置成功", Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    // 两次密码不一致
                    Toast.makeText(this, "两次输入的密码不一致，请重新输入", Toast.LENGTH_SHORT).show();
                    firstNewPassword = null;
                    password.setLength(0);
                    updatePasswordDots(0);
                    updateUI();
                }
            }
        }
    }

    @Override
    public void onBackPressed() {
        // 允许返回
        super.onBackPressed();
    }
}

